#include "gitleventbus.h"
#include "gitliomsg.h"

SINGLETON_PATTERN_IMPLIMENT(GitlEventBus)

GitlEventBus::GitlEventBus() :
    m_cBlockEvtDispatcher(&m_cModuals),
    m_cNonBlockEvtDispatcher(&m_cModuals)
{
    m_cBlockEvtDispatcher.start();
}

GitlEventBus::~GitlEventBus()
{
}


/*! connect a modual to the event bus
  */
bool GitlEventBus::registerModual(GitlModual* pcModual)
{
    QMutexLocker cModualLocker(&m_cModualQueLocker);
    for(int iModualCount = 0; iModualCount < m_cModuals.size(); iModualCount++ )
    {
        if( pcModual == m_cModuals.at(iModualCount) )
            return false;
    }
    m_cModuals.push_back(pcModual);
    return true;
}

/*! disconnect a modual from the event bus
  *
  */
bool GitlEventBus::unregisterModual(GitlModual* pcModual)
{
    QMutexLocker cModualLocker(&m_cModualQueLocker);
    for(int iModualCount = 0; iModualCount < m_cModuals.size(); iModualCount++ )
    {
        if( pcModual == m_cModuals.at(iModualCount) )
        {
            m_cModuals.removeAt(iModualCount);
            return true;
        }

    }
    return false;

}

/*! send event to event bus
  */
void GitlEventBus::post(GitlEvent* pcEvt)
{
    if( pcEvt->getEvtType() == BLOCKING )
    {
        m_cBlockEvtDispatcher.postEvent(pcEvt);
    }
    else
    {
        m_cNonBlockEvtDispatcher.postEvent(pcEvt);
    }
}

